package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.annotation.TelAndPwdCheck;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.JWTUtil;
import com.woniu.car.config.MyRedisTemplate;
import com.woniu.car.domain.TUserAccount;
import com.woniu.car.dto.TelCodeDto;
import com.woniu.car.dto.UserAccountDto;
import com.woniu.car.dto.UserRegisterDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.feign.UserClient;
import com.woniu.car.mapper.TUserAccountMapper;
import com.woniu.car.param.*;
import com.woniu.car.service.TUserAccountService;
import com.woniu.car.util.AccountStatus;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Service
public class TUserAccountServiceImpl extends ServiceImpl<TUserAccountMapper, TUserAccount> implements TUserAccountService {
    @Resource
    private MyRedisTemplate myRedisTemplate;
    @Resource
    private TUserAccountMapper tUserAccountMapper;
    @Resource
    private UserClient userClient;
    /**
     *@Author 王维鑫
     *@Description 新增用户账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TUserAccount insertUserAccount(TUserAccount tUserAccount)  {
        System.out.println("第一次："+tUserAccount);
        boolean save = this.save(tUserAccount);
        if(save==false){
            throw new AccountException("新增用户账号失败");
        }
        return tUserAccount;
    }
    /**
     *@Author 王维鑫
     *@Description 修改用户账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void updateUserAccount(TUserAccount tUserAccount)  {
        boolean b = this.updateById(tUserAccount);
        if(b==false){
            throw new AccountException("修改用户账号失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 查寻用户
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TUserAccount> queryUserAccount(TUserAccount tUserAccount) {
        QueryWrapper<TUserAccount> wrapper = new QueryWrapper<>();
        if(null!=tUserAccount.getUserId()){
            wrapper.eq("user_id",tUserAccount.getUserId());
        }
        if(null!=tUserAccount.getUserTel()){
            wrapper.eq("user_tel",tUserAccount.getUserTel());
        }
        if(null!=tUserAccount.getUserStatus()){
            wrapper.eq("user_status",tUserAccount.getUserStatus());
        }
        if(null!=tUserAccount.getRole()){
            wrapper.eq("role",tUserAccount.getRole());
        }
        List<TUserAccount> list = this.list(wrapper);
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 用户账号注册
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public UserRegisterDto registerUser(UserRegisterParam param)  {
        TUserAccount tUserAccount = new TUserAccount();
        tUserAccount.setUserTel(param.getUserTel());
        List<TUserAccount> tUserAccounts = this.queryUserAccount(tUserAccount);
        if(tUserAccounts.size()==0){
            BeanCopyUtils.copyProperties(param,tUserAccount);
            tUserAccount.setUserStatus(AccountStatus.NORMAL);
            Date date = new Date();
            long time = date.getTime();
            tUserAccount.setRegistTime(time);
            TUserAccount tUserAccount1 = this.insertUserAccount(tUserAccount);
            UserRegisterDto userAccountDto = new UserRegisterDto();
            BeanCopyUtils.copyProperties(tUserAccount1,userAccountDto);
            UserInfoFeignParam userInfoFeignParam = new UserInfoFeignParam();
            userInfoFeignParam.setUserId(userAccountDto.getUserId());
            userInfoFeignParam.setTel(userAccountDto.getUserTel());
            userClient.addUserInfo(userInfoFeignParam);
            return userAccountDto;
        }else {
            throw new AccountException("该手机号已注册，请更换手机号");
        }
    }

    /**
     *@Author 王维鑫
     *@Description 发送短息
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TelCodeDto sendCode(String tel)  {
                Integer code=(int)((Math.random()*9+1)*100000);
                ValueOperations<String, Object> value = myRedisTemplate.opsForValue();
                value.set("code:"+tel,code);
                myRedisTemplate.expire("code:"+tel,60, TimeUnit.SECONDS);
                TelCodeDto telCodeDto = new TelCodeDto();
                telCodeDto.setCode(code.toString());
                telCodeDto.setTel(tel);
                return telCodeDto;

    }

    /**
     *@Author 王维鑫
     *@Description 用户账号密码登录
     *@Param
     *@return
     **/
    @TelAndPwdCheck
    @Override
    @Transactional
    public UserAccountDto loginByPwd(UserLoginByPwdParam param)  {
        TUserAccount tUserAccount = new TUserAccount();
        tUserAccount.setUserTel(param.getUserTel());
        List<TUserAccount> tUserAccounts1 = this.queryUserAccount(tUserAccount);
        if(tUserAccounts1.size()==0){
            throw new AccountException("账号不存在");
        }else {
            TUserAccount tUserAccount1 = tUserAccounts1.get(0);
            if(!param.getUserPwd().equals(tUserAccount1.getUserPwd())){
                throw new AccountException("密码错误");
            }else {
                long time = new Date().getTime();
                tUserAccount1.setLoginTime(time);
                this.updateUserAccount(tUserAccount1);
                UserAccountDto userAccountDto = new UserAccountDto();
                BeanCopyUtils.copyProperties(tUserAccounts1.get(0),userAccountDto);
                String token = JWTUtil.generatorUserJWT(tUserAccounts1.get(0).getUserId().toString());
                userAccountDto.setToken(token);
                return userAccountDto;
            }
        }
    }
    /**
     *@Author 王维鑫
     *@Description 短信登录
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public UserAccountDto loginByCode(UserLoginByCodeParam param) {
        TUserAccount tUserAccount = new TUserAccount();
        tUserAccount.setUserTel(param.getUserTel());
        List<TUserAccount> tUserAccounts = this.queryUserAccount(tUserAccount);
        if(tUserAccounts.size()==0){
            throw new AccountException("该账号未注册");
        }else {
            Long date = new Date().getTime();
            tUserAccounts.get(0).setLoginTime(date);
            this.updateUserAccount(tUserAccounts.get(0));
            UserAccountDto userAccountDto = new UserAccountDto();
            String token = JWTUtil.generatorUserJWT(tUserAccounts.get(0).getUserId().toString());
            BeanCopyUtils.copyProperties(tUserAccounts.get(0),userAccountDto);
            userAccountDto.setToken(token);
            return userAccountDto;
        }
    }
    /**
     *@Author 王维鑫
     *@Description 根据ID查询用户
     *@Param
     *@return
     **/
    @Override
    public UserAccountDto queryUserById(Integer userId) {
        TUserAccount tUserAccount = tUserAccountMapper.selectById(userId);
        if(null==tUserAccount){
            throw new AccountException("未找到该用户");
        }
        UserAccountDto userAccountDto = new UserAccountDto();
        BeanCopyUtils.copyProperties(tUserAccount,userAccountDto);
        return userAccountDto;
    }


}
